<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/16
 * Time: 11:15
 */

namespace zhtfrontend\controllers;

use doumiaoduo\buildform\BuildForm;
use yii\widgets\ActiveForm;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\CommentsInfo;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\BadRequestHttpException;
use yii\data\Pagination;
use yii\helpers\Json;

class EvaluateController extends BaseController
{


    //public $layout='@app/views/layouts/iframe.php';

    /*
     * 评价项列表
     *
     * */
    public function actionIndex()
    {
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $commentinfos = CommentsInfo::find()->orderBy('id DESC');
        $pages = new Pagination(['totalCount' => $commentinfos->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $commentinfos = $commentinfos->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($commentinfos, ['operate', 'user', 'lastuser']);

        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }

    /*
     * 评价项添加
     * 2017-5-16
     * */
    public function actionCreate()
    {
        $this->layout = false;
        $model = new CommentsInfo();
        $form = new BuildForm('@app/components/forms/commentsInfoForm.php');
        $form->loadModel(['commentsInfo' => $model]);
        $form->loadDefaultValues();

        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'commentsInfo-form' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->user_id = \Yii::$app->user->id;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['evaluate/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    /*
     * 评价项修改
     * 2017-5-8
     * */
    public function actionUpdate()
    {
        $this->layout = false;
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('frontend', '信息不存在'));
        }
        $model = CommentsInfo::findOne(\Yii::$app->request->get('id'));
        $form = new BuildForm('@app/components/forms/commentsInfoForm.php');
        $form->loadModel(['commentsInfo' => $model]);
        $form->loadDefaultValues();
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('frontend', '信息不存在'));
        }
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'commentsInfo-form' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if ($model->save(false)) {
                return $this->redirectMessage('修改成功', Fun::url(['evaluate/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }
}
